package com.awspaas.user.apps.shhtaerospaceindustrial.event;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.actionsoft.bpms.api.OpenApiClient;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.schedule.IJob;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.service.response.BoolResponse;

public class CleanInvalidVistorInfo implements IJob {
	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

		String apiServer = "https://www.sht808.com/portal/openapi";

		String accessKey = "opapiuser4326";
		String secret = "Abcd123456@#";

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Calendar calendar = new GregorianCalendar();
		Date now = new Date();
		calendar.setTime(now);
		calendar.add(Calendar.DATE, -2);
		Date endDate = calendar.getTime();
		String endTime = endDate.toLocaleString();
		
		calendar.add(Calendar.DATE, -1);
		Date endDate2 = calendar.getTime();
		String endTime2 = endDate.toLocaleString();
		
//		calendar.setTime(now);
//		calendar.add(calendar.DATE, -1);
//		Date endDate1 = calendar.getTime();
//		String endTime1 = endDate.toLocaleString();
		
		Map<String, Object> args = new HashMap<String, Object>();
		OpenApiClient client = new OpenApiClient(apiServer, accessKey, secret);
		
		/***
		 * 查询并删除过期访客自助实例信息
		 */
		System.out.println("Delete useless visitor self info...");
		String visitorQuery = "select  t.id from wfc_process t, wfc_task a where t.processdefid = 'obj_275bed151e5840d08e10e518644de33a' and t.id = a.processinstid and a.activitydefid in ('obj_c96b8faf04200001554411a9159c15af','obj_c96b8fb14c80000139c315741e71162e')  and t.createtime < to_date('"
				+ endTime + "','yyyy-MM-dd HH24:MI:SS')";
		//System.out.println(visitorQuery);
		List<Map<String, Object>> processList = new ArrayList<Map<String, Object>>();



		processList = DBSql.query(visitorQuery, new ColumnMapRowMapper(), new Object[] {});
		for (int i = 0; i < processList.size(); i++) {
			String id = processList.get(i).get("ID").toString();
			System.out.println("Delete zz visitor process："+id);
			args.put("processInstId", id);
			args.put("uid", "admin");
			BoolResponse r = client.exec("process.delete", args, BoolResponse.class);
		}
		
		
		
		/***
		 * 查询并删除过期员工代理访客实例信息
		 */
		System.out.println("Delete useless visitor info applied by employee...");
		String employQuery = "select  t.id from wfc_process t, wfc_task a where t.processdefid = 'obj_a5d447ce1ef94d418ccbda4eabd766ba' and t.id = a.processinstid and a.activitydefid = 'obj_c970323398a000012e681a307740e300' and t.createtime < to_date('"
				+ endTime + "','yyyy-MM-dd HH24:MI:SS')";
		//System.out.println(visitorQuery);
		List<Map<String, Object>> processEmList = new ArrayList<Map<String, Object>>();
		processEmList = DBSql.query(employQuery, new ColumnMapRowMapper(), new Object[] {});
		for (int i = 0; i < processEmList.size(); i++) {
			String id = processEmList.get(i).get("ID").toString();
			System.out.println("Delete visitor process："+id);
			args.put("processInstId", id);
			args.put("uid", "admin");
			BoolResponse r = client.exec("process.delete", args, BoolResponse.class);
		}
		
		/***
		 * 查询并删除过期149员工代理访客实例信息
		 */
		System.out.println("Delete useless visitor info applied by employee...");
		String employQuery149 = "select  t.id from wfc_process t, wfc_task a where t.processdefid = 'obj_32eccfa333414635bc4b021e768e2424' and t.id = a.processinstid and a.activitydefid in ('obj_ca0fdda777300001d9c41e3b1aac7a50','obj_ca1071378e5000013b42109aa69b1a80') and t.createtime < to_date('"
				+ endTime + "','yyyy-MM-dd HH24:MI:SS') ";
		//System.out.println(visitorQuery);
		List<Map<String, Object>> processEmList149 = new ArrayList<Map<String, Object>>();
		processEmList149 = DBSql.query(employQuery149, new ColumnMapRowMapper(), new Object[] {});
		for (int i = 0; i < processEmList149.size(); i++) {
			String id = processEmList149.get(i).get("ID").toString();
			System.out.println("Delete 149 visitor process："+id);
			args.put("processInstId", id);
			args.put("uid", "admin");
			BoolResponse r = client.exec("process.delete", args, BoolResponse.class);
		}
		
		/***
		 * 查询超过3天还未提交报修流程实例信息
		 */
		System.out.println("Delete useless repair  info applied by employee...");
		String employQueryforrepair = "select  t.id from wfc_process t, wfc_task a where t.processdefid = 'obj_b6b2e3b8132e4c05bd0e2c9c574f2977' and t.id = a.processinstid  and a.activitydefid in ('obj_858e5ba5cbad4627a0392e8fa4f11758') and t.createtime < to_date('"
				+ endTime2 + "','yyyy-MM-dd HH24:MI:SS')  ";
		//System.out.println(visitorQuery);
		List<Map<String, Object>> processover3dayrepairList = new ArrayList<Map<String, Object>>();
		processover3dayrepairList = DBSql.query(employQueryforrepair, new ColumnMapRowMapper(), new Object[] {});
		for (int i = 0; i < processover3dayrepairList.size(); i++) {
			String id = processover3dayrepairList.get(i).get("ID").toString();
			System.out.println("Delete repair process："+id);
			args.put("processInstId", id);
			args.put("uid", "admin");
			BoolResponse r = client.exec("process.delete", args, BoolResponse.class);
		}
		
		/***
		 * 查询超过3天还未提交车辆预定流程实例信息
		 */
		System.out.println("Delete useless repair  info applied by employee...");
		String employQueryforcarorder = "select  t.id from wfc_process t, wfc_task a where t.processdefid = 'obj_b958c0c1ee3e4d05848413ed5ea2e2c4' and t.id = a.processinstid  and a.activitydefid in ('obj_17e36dee86154dfda0bff2e4f6bf2cf2') and t.createtime < to_date('"
				+ endTime2 + "','yyyy-MM-dd HH24:MI:SS')  ";
		//System.out.println(visitorQuery);
		List<Map<String, Object>> processover3daycarorderList = new ArrayList<Map<String, Object>>();
		processover3daycarorderList = DBSql.query(employQueryforcarorder, new ColumnMapRowMapper(), new Object[] {});
		for (int i = 0; i < processover3daycarorderList.size(); i++) {
			String id = processover3daycarorderList.get(i).get("ID").toString();
			System.out.println("Delete carodrder process："+id);
			args.put("processInstId", id);
			args.put("uid", "admin");
			BoolResponse r = client.exec("process.delete", args, BoolResponse.class);
		}
		
		/***
		 * 查询超过3天还未提交意见反馈流程实例信息
		 */
		System.out.println("Delete useless advice  info applied by employee...");
		String employQueryforadvice = "select  t.id from wfc_process t, wfc_task a where t.processdefid = 'obj_b8c81bcf702b40acbdf1662b7da16d5f' and t.id = a.processinstid  and a.activitydefid in ('obj_c906cd3584800001f24c6000113a51f0') and t.createtime < to_date('"
				+ endTime2 + "','yyyy-MM-dd HH24:MI:SS')  ";
		//System.out.println(visitorQuery);
		List<Map<String, Object>> processover3dayadviceList = new ArrayList<Map<String, Object>>();
		processover3dayadviceList = DBSql.query(employQueryforadvice, new ColumnMapRowMapper(), new Object[] {});
		for (int i = 0; i < processover3dayadviceList.size(); i++) {
			String id = processover3dayadviceList.get(i).get("ID").toString();
			System.out.println("Delete advice process："+id);
			args.put("processInstId", id);
			args.put("uid", "admin");
			BoolResponse r = client.exec("process.delete", args, BoolResponse.class);
		}
		
		
		/***
		 * 查询并删除过期149访客自助实例信息
		 */
		System.out.println("Delete useless visitor self info...");
		
		String visitorQuery149 = "select  t.id from wfc_process t, wfc_task a where t.processdefid = 'obj_52e4446bb3c04b088fae1605a17bccbb' and t.id = a.processinstid  and a.activitydefid in ('obj_ca0fde346eb0000171cff6b010856290','obj_ca0fde37afe000015a551cf018801dbf','obj_ca10b2fb93e0000186a71a6cebd92b30') and t.createtime < to_date('"
				+ endTime2 + "','yyyy-MM-dd HH24:MI:SS')   ";
		//System.out.println(visitorQuery);
		List<Map<String, Object>> processList149 = new ArrayList<Map<String, Object>>();



		processList149 = DBSql.query(visitorQuery149, new ColumnMapRowMapper(), new Object[] {});
		for (int i = 0; i < processList149.size(); i++) {
			String id = processList149.get(i).get("ID").toString();
			System.out.println("Delete  149zz visitor process："+id);
			args.put("processInstId", id);
			args.put("uid", "admin");
			BoolResponse r = client.exec("process.delete", args, BoolResponse.class);
		}
		

	}

}
